package com.platform.api.film.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.api.bzproject.model.BzAllWallpaper;
import com.platform.api.bzproject.service.BzAllWallpaperService;
import com.platform.api.film.dao.FilmPlayRecordMapper;
import com.platform.api.film.model.FilmPlayRecord;
import com.platform.api.film.service.FilmPlayRecordService;
import com.platform.api.film.vo.PlayVo;
import com.platform.common.exception.PlatformBindException;
import com.platform.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author guoyunfei
 */
@Service
public class FilmPlayRecordServiceImpl extends ServiceImpl<FilmPlayRecordMapper,FilmPlayRecord> implements FilmPlayRecordService {

    @Autowired
    private BzAllWallpaperService bzAllWallpaperService;

    @Override
    public List<FilmPlayRecord> getListByFilmId(Long filmId) {
        List<FilmPlayRecord> records = baseMapper.selectList(new LambdaQueryWrapper<FilmPlayRecord>()
                .eq(FilmPlayRecord::getUserId, SecurityUtils.getUserId())
                .eq(FilmPlayRecord::getFilmId, filmId)
                .select(FilmPlayRecord::getId,FilmPlayRecord::getFilmId,FilmPlayRecord::getResourceId));
        return records;
    }

    @Override
    public List<Long> getPlayIds(Long filmId) {
        List<FilmPlayRecord> list = getListByFilmId(filmId);
        List<Long> playIds = list.stream().map(FilmPlayRecord::getResourceId).collect(Collectors.toList());
        return playIds;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addOrUpdate(Long resourceId) {

        FilmPlayRecord filmPlayRecord = new FilmPlayRecord();
        filmPlayRecord.setResourceId(resourceId);
        Date date = new Date();

        BzAllWallpaper wallpaper = bzAllWallpaperService.getById(filmPlayRecord.getResourceId());
        if(Objects.isNull(wallpaper)){
            throw new PlatformBindException("资源不存在");
        }

        FilmPlayRecord record = getByComWhere(wallpaper.getFilmId(), filmPlayRecord.getResourceId());

        if(Objects.isNull(record)){
            //为空则新增
            filmPlayRecord.setUserId(SecurityUtils.getUserId());
            filmPlayRecord.setFilmId(wallpaper.getFilmId());
            filmPlayRecord.setCreateTime(date);
            filmPlayRecord.setUpdateTime(date);
            filmPlayRecord.setLastTime(date);
            return this.save(filmPlayRecord);
        }else{
            //此处修改时间为了用于定位最后看的是那个资源
            record.setLastTime(date);
            record.setUpdateTime(date);
        }
        return this.updateById(record);
    }

    /**
     * 查询记录
     * @param filmId 影视ID
     * @param resourceId 资源ID
     * @return
     */
    @Override
    public FilmPlayRecord getByComWhere(Long filmId, Long resourceId) {
        FilmPlayRecord playRecord = baseMapper.selectOne(new LambdaQueryWrapper<FilmPlayRecord>()
                .eq(FilmPlayRecord::getFilmId, filmId).eq(FilmPlayRecord::getResourceId, resourceId)
                .eq(FilmPlayRecord::getUserId, SecurityUtils.getUserId()));
        return playRecord;
    }

    /**
     * 获取当前用户下的最后播放的一条记录
     * @param filmId 影视ID
     * @return
     */
    @Override
    public FilmPlayRecord getLastRecord(Long filmId) {
        FilmPlayRecord playRecord = baseMapper.selectOne(new LambdaQueryWrapper<FilmPlayRecord>()
                .eq(FilmPlayRecord::getFilmId, filmId)
                .eq(FilmPlayRecord::getUserId, SecurityUtils.getUserId())
                .orderByDesc(FilmPlayRecord::getLastTime)
                .last("limit 1"));
        if(Objects.isNull(playRecord)){
            throw new PlatformBindException("无播放记录");
        }
        return playRecord;
    }

    /**
     * 获取的播放记录
     * @return
     */
    @Override
    public IPage<PlayVo> filmPlayRecordPage(Page page) {
        IPage<PlayVo> list = baseMapper.filmPlayRecordPage(page, SecurityUtils.getUserId());
        return list;
    }
}
